unit config;
(*  cssMonkey
    Copyright (C) 2011 Matthew Hipkin <http://www.matthewhipkin.co.uk>

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, SynEdit, SynHighlighterCss, Forms, Controls,
  Graphics, Dialogs, StdCtrls, EditBtn, Buttons, Spin;

type

  { TfrmOptions }

  TfrmOptions = class(TForm)
    btnOK: TButton;
    btnCancel: TButton;
    btnItemColour: TColorButton;
    btnBgColour: TColorButton;
    listItems: TComboBox;
    dialogFont: TFontDialog;
    Label3: TLabel;
    editPreview: TSynEdit;
    textFontSize: TSpinEdit;
    textFont: TEditButton;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label2: TLabel;
    highlighterCSS: TSynCssSyn;
    btnBold: TToggleBox;
    btnItalic: TToggleBox;
    btnUnderline: TToggleBox;
    procedure btnBgColourColorChanged(Sender: TObject);
    procedure btnBoldClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure btnItalicChange(Sender: TObject);
    procedure btnItemColourColorChanged(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure btnUnderlineChange(Sender: TObject);
    procedure listItemsSelect(Sender: TObject);
    procedure textFontButtonClick(Sender: TObject);
    procedure textFontSizeChange(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  frmOptions: TfrmOptions;

implementation

{$R *.lfm}

{ TfrmOptions }

procedure TfrmOptions.textFontButtonClick(Sender: TObject);
begin
  if dialogFont.Execute then
  begin
    textFont.Text := dialogFont.Font.Name;
    textFontSize.Value := dialogFont.Font.Size;
    editPreview.Font := dialogFont.Font;
  end;
end;

procedure TfrmOptions.textFontSizeChange(Sender: TObject);
begin
  editPreview.Font.Size := textFontSize.Value;
  dialogFont.Font.Size := textFontSize.Value;
end;

procedure TfrmOptions.btnBgColourColorChanged(Sender: TObject);
begin
  editPreview.Color := btnBgColour.ButtonColor;
end;


procedure TfrmOptions.listItemsSelect(Sender: TObject);
begin
  case listItems.ItemIndex of
    0: begin
         btnItemColour.ButtonColor := clNone;
         btnBold.State := cbUnchecked;
         btnItalic.State := cbUnchecked;
         btnUnderline.State := cbUnchecked;
       end;
    1: begin
         btnItemColour.ButtonColor:=highlighterCSS.CommentAttri.Foreground;
         if fsBold in highlighterCSS.CommentAttri.Style then btnBold.State :=  cbChecked else btnBold.State := cbUnchecked;
         if fsItalic in highlighterCSS.CommentAttri.Style then btnItalic.State :=  cbChecked else btnItalic.State := cbUnchecked;
         if fsUnderline in highlighterCSS.CommentAttri.Style then btnUnderline.State :=  cbChecked else btnUnderline.State := cbUnchecked;
       end;
    2: begin
         btnItemColour.ButtonColor:=highlighterCSS.IdentifierAttri.Foreground;
         if fsBold in highlighterCSS.IdentifierAttri.Style then btnBold.State :=  cbChecked else btnBold.State := cbUnchecked;
         if fsItalic in highlighterCSS.IdentifierAttri.Style then btnItalic.State :=  cbChecked else btnItalic.State := cbUnchecked;
         if fsUnderline in highlighterCSS.IdentifierAttri.Style then btnUnderline.State :=  cbChecked else btnUnderline.State := cbUnchecked;
       end;
    3: begin
         btnItemColour.ButtonColor:=highlighterCSS.KeyAttri.Foreground;
         if fsBold in highlighterCSS.KeyAttri.Style then btnBold.State :=  cbChecked else btnBold.State := cbUnchecked;
         if fsItalic in highlighterCSS.KeyAttri.Style then btnItalic.State :=  cbChecked else btnItalic.State := cbUnchecked;
         if fsUnderline in highlighterCSS.KeyAttri.Style then btnUnderline.State :=  cbChecked else btnUnderline.State := cbUnchecked;
       end;
    4: begin
         btnItemColour.ButtonColor:=highlighterCSS.NumberAttri.Foreground;
         if fsBold in highlighterCSS.NumberAttri.Style then btnBold.State :=  cbChecked else btnBold.State := cbUnchecked;
         if fsItalic in highlighterCSS.NumberAttri.Style then btnItalic.State :=  cbChecked else btnItalic.State := cbUnchecked;
         if fsUnderline in highlighterCSS.NumberAttri.Style then btnUnderline.State :=  cbChecked else btnUnderline.State := cbUnchecked;
       end;
    5: begin
         btnItemColour.ButtonColor:=highlighterCSS.StringAttri.Foreground;
         if fsBold in highlighterCSS.StringAttri.Style then btnBold.State :=  cbChecked else btnBold.State := cbUnchecked;
         if fsItalic in highlighterCSS.StringAttri.Style then btnItalic.State :=  cbChecked else btnItalic.State := cbUnchecked;
         if fsUnderline in highlighterCSS.StringAttri.Style then btnUnderline.State :=  cbChecked else btnUnderline.State := cbUnchecked;
       end;
    6: begin
         btnItemColour.ButtonColor:=highlighterCSS.SymbolAttri.Foreground;
         if fsBold in highlighterCSS.SymbolAttri.Style then btnBold.State :=  cbChecked else btnBold.State := cbUnchecked;
         if fsItalic in highlighterCSS.SymbolAttri.Style then btnItalic.State :=  cbChecked else btnItalic.State := cbUnchecked;
         if fsUnderline in highlighterCSS.SymbolAttri.Style then btnUnderline.State :=  cbChecked else btnUnderline.State := cbUnchecked;
       end;
  end;
end;

procedure TfrmOptions.btnItemColourColorChanged(Sender: TObject);
begin
  case listItems.ItemIndex of
    0: exit;
    1: highlighterCSS.CommentAttri.Foreground := btnItemColour.ButtonColor;
    2: highlighterCSS.IdentifierAttri.Foreground := btnItemColour.ButtonColor;
    3: highlighterCSS.KeyAttri.Foreground := btnItemColour.ButtonColor;
    4: highlighterCSS.NumberAttri.Foreground := btnItemColour.ButtonColor;
    5: highlighterCSS.StringAttri.Foreground := btnItemColour.ButtonColor;
    6: highlighterCSS.SymbolAttri.Foreground := btnItemColour.ButtonColor;
  end;
end;

procedure TfrmOptions.btnOKClick(Sender: TObject);
begin
  ModalResult := mrOK;
end;

procedure TfrmOptions.btnCancelClick(Sender: TObject);
begin
  modalResult := mrCancel;
end;

procedure TfrmOptions.btnUnderlineChange(Sender: TObject);
begin
  case listItems.ItemIndex of
    0: exit;
    1: if btnUnderline.State = cbChecked then highlighterCSS.CommentAttri.Style := highlighterCSS.CommentAttri.Style + [fsUnderline] else highlighterCSS.CommentAttri.Style := highlighterCSS.CommentAttri.Style - [fsUnderline];
    2: if btnUnderline.State = cbChecked then highlighterCSS.IdentifierAttri.Style := highlighterCSS.IdentifierAttri.Style + [fsUnderline] else highlighterCSS.IdentifierAttri.Style := highlighterCSS.IdentifierAttri.Style - [fsUnderline];
    3: if btnUnderline.State = cbChecked then highlighterCSS.KeyAttri.Style := highlighterCSS.KeyAttri.Style + [fsUnderline] else highlighterCSS.KeyAttri.Style := highlighterCSS.KeyAttri.Style - [fsUnderline];
    4: if btnUnderline.State = cbChecked then highlighterCSS.NumberAttri.Style := highlighterCSS.NumberAttri.Style + [fsUnderline] else highlighterCSS.NumberAttri.Style := highlighterCSS.NumberAttri.Style - [fsUnderline];
    5: if btnUnderline.State = cbChecked then highlighterCSS.StringAttri.Style := highlighterCSS.StringAttri.Style + [fsUnderline] else highlighterCSS.StringAttri.Style := highlighterCSS.StringAttri.Style - [fsUnderline];
    6: if btnUnderline.State = cbChecked then highlighterCSS.SymbolAttri.Style := highlighterCSS.SymbolAttri.Style + [fsUnderline] else highlighterCSS.SymbolAttri.Style := highlighterCSS.SymbolAttri.Style - [fsUnderline];
  end;
end;

procedure TfrmOptions.btnBoldClick(Sender: TObject);
begin
  case listItems.ItemIndex of
    0: exit;
    1: if btnBold.State = cbChecked then highlighterCSS.CommentAttri.Style := highlighterCSS.CommentAttri.Style + [fsBold] else highlighterCSS.CommentAttri.Style := highlighterCSS.CommentAttri.Style - [fsBold];
    2: if btnBold.State = cbChecked then highlighterCSS.IdentifierAttri.Style := highlighterCSS.IdentifierAttri.Style + [fsBold] else highlighterCSS.IdentifierAttri.Style := highlighterCSS.IdentifierAttri.Style - [fsBold];
    3: if btnBold.State = cbChecked then highlighterCSS.KeyAttri.Style := highlighterCSS.KeyAttri.Style + [fsBold] else highlighterCSS.KeyAttri.Style := highlighterCSS.KeyAttri.Style - [fsBold];
    4: if btnBold.State = cbChecked then highlighterCSS.NumberAttri.Style := highlighterCSS.NumberAttri.Style + [fsBold] else highlighterCSS.NumberAttri.Style := highlighterCSS.NumberAttri.Style - [fsBold];
    5: if btnBold.State = cbChecked then highlighterCSS.StringAttri.Style := highlighterCSS.StringAttri.Style + [fsBold] else highlighterCSS.StringAttri.Style := highlighterCSS.StringAttri.Style - [fsBold];
    6: if btnBold.State = cbChecked then highlighterCSS.SymbolAttri.Style := highlighterCSS.SymbolAttri.Style + [fsBold] else highlighterCSS.SymbolAttri.Style := highlighterCSS.SymbolAttri.Style - [fsBold];
  end;
end;

procedure TfrmOptions.btnItalicChange(Sender: TObject);
begin
  case listItems.ItemIndex of
    0: exit;
    1: if btnItalic.State = cbChecked then highlighterCSS.CommentAttri.Style := highlighterCSS.CommentAttri.Style + [fsItalic] else highlighterCSS.CommentAttri.Style := highlighterCSS.CommentAttri.Style - [fsItalic];
    2: if btnItalic.State = cbChecked then highlighterCSS.IdentifierAttri.Style := highlighterCSS.IdentifierAttri.Style + [fsItalic] else highlighterCSS.IdentifierAttri.Style := highlighterCSS.IdentifierAttri.Style - [fsItalic];
    3: if btnItalic.State = cbChecked then highlighterCSS.KeyAttri.Style := highlighterCSS.KeyAttri.Style + [fsItalic] else highlighterCSS.KeyAttri.Style := highlighterCSS.KeyAttri.Style - [fsItalic];
    4: if btnItalic.State = cbChecked then highlighterCSS.NumberAttri.Style := highlighterCSS.NumberAttri.Style + [fsItalic] else highlighterCSS.NumberAttri.Style := highlighterCSS.NumberAttri.Style - [fsItalic];
    5: if btnItalic.State = cbChecked then highlighterCSS.StringAttri.Style := highlighterCSS.StringAttri.Style + [fsItalic] else highlighterCSS.StringAttri.Style := highlighterCSS.StringAttri.Style - [fsItalic];
    6: if btnItalic.State = cbChecked then highlighterCSS.SymbolAttri.Style := highlighterCSS.SymbolAttri.Style + [fsItalic] else highlighterCSS.SymbolAttri.Style := highlighterCSS.SymbolAttri.Style - [fsItalic];
  end;
end;

end.

